package com.ys;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.poi.excel.cell.CellUtil;
import com.google.zxing.WriterException;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.ys.enums.ExportEnum;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Table;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class InvoiceExporter {

    public void export(Map<String, Object> head, Map<String, Object> list, int pageSize, ExportEnum type) throws IOException {
        InvoiceHeader header = convertToHeader(list);
        List<InvoiceItem> items = convertToList(list);

        if (ExportEnum.EXCEL.equals(type)) {
            exportToExcel(header, items, pageSize);
        } else if (ExportEnum.PDF.equals(type)) {
            exportToPdf(header, items, pageSize);
        }
    }

    private InvoiceHeader convertToHeader(Map<String, Object> head) {
        InvoiceHeader header = new InvoiceHeader();
        header.setInvoiceDate((String) head.get("invoiceDate"));
        header.setRecipient((String) head.get("recipient"));
        header.setOrderNo((String) head.get("orderNo"));
        header.setAddress((String) head.get("address"));
        header.setPageCoinNum(ObjectUtils.toString(head.get("pageCoinNum")));
        header.setPagecountS(ObjectUtils.toString( head.get("pagecountS")));
        header.setTotalCount(ObjectUtils.toString(head.get("totalCount")));
        header.setAllTotalCount(ObjectUtils.toString(head.get("allTotalCount")));
        header.setTotalNum(ObjectUtils.toString(head.get("totalNum")));
        header.setPageCountNum(ObjectUtils.toString(head.get("pageCountNum")));
        header.setTemplateFileInfo((TemplateFileInfo) head.get("templateFileInfo"));
        return header;
    }

    private List<InvoiceItem> convertToList(Map<String, Object> maps) {
        List<Map<String, Object>> datas = (List<Map<String, Object>>) maps.get("datas");
        List<InvoiceItem> items = new ArrayList<>();
        datas.forEach(map -> {
            InvoiceItem item = new InvoiceItem();
            item.setId(ObjectUtils.toString(map.get("id")));
            item.setProductName((String) map.get("name"));
            item.setSpecification((String) map.get("guige"));
            item.setQuantity(String.valueOf(map.get("num")));
            item.setUnitPrice(String.valueOf(map.get("price")));
            item.setTotal(String.valueOf(map.get("totalCash")));
            item.setEntrustCompany(((String) map.get("entrustCompany")));
            item.setProductCompany(((String) map.get("productCompany")));
            item.setProductLicence(((String) map.get("productLicence")));
            item.setSerialNumber(((String) map.get("serialNumber")));
            item.setManufactureDate(((String) map.get("manufactureDate")));
            item.setExpirationDate(((String) map.get("expirationDate")));
            item.setConsignee(((String) map.get("consignee")));
            item.setConditions(((String) map.get("conditions")));
            items.add(item);
        });
        return items;
    }

    private void exportToExcel(InvoiceHeader header, List<InvoiceItem> items, int pageSize) throws IOException {
        try (Workbook workbook = new XSSFWorkbook()) {
            int dataRowIndex = 5;
            int currentRowIndex = 5;
            Sheet sheet = null;
            for (int i = 0; i < items.size(); i++) {
                Integer pageNum = (items.size() + pageSize - 1) / pageSize;
                if (i > 0 && i % pageSize == 0) {
                    sheet = workbook.createSheet("Invoice - " + (i / pageSize + 1));
                    sheet.setColumnWidth(0,15*256);
                    sheet.setColumnWidth(5,30*256);
                    sheet.setColumnWidth(6,30*256);
                    sheet.setColumnWidth(7,30*256);
                    sheet.setColumnWidth(8,15*256);
                    sheet.setColumnWidth(9,15*256);
                    sheet.setColumnWidth(10,15*256);
                    Row row0 = sheet.createRow(0);
                    row0.setHeight((short) (30 * 20));
                    CellUtil.mergingCells(sheet, 0, 0, 0, 12, null);
                    Cell cell = row0.createCell(0);
                    CellStyle cellStyle = workbook.createCellStyle();
                    cellStyle.setAlignment(HorizontalAlignment.CENTER);
                    Font font = workbook.createFont();
                    font.setBold(true);
                    font.setFontHeightInPoints((short) 22);
                    cellStyle.setFont(font);
                    cell.setCellValue("供应链管理有限公司销售出库单");
                    cell.setCellStyle(cellStyle);
                    // Write header information
                    Row headerRow = sheet.createRow(1);
                    headerRow.setHeight((short) (25 * 20));
                    CellUtil.mergingCells(sheet, 1, 1, 1, 5, null);
                    createCell(headerRow, 0, "开票日期: ",false);
                    createCell(headerRow, 1, header.getInvoiceDate(),false);
                    createCell(headerRow, 10, "第 " +(i / pageSize + 1) + "/"+pageNum + " 页",false);
                    Row headerRow2 = sheet.createRow(2);
                    headerRow2.setHeight((short) (25 * 20));
                    CellUtil.mergingCells(sheet, 2, 2, 1, 5, null);
                    createCell(headerRow2, 0, "单位名称: ",false);
                    createCell(headerRow2, 1,  header.getRecipient(),false);
                    createCell(headerRow2, 7, "收货地址: " + header.getAddress(),false);
                    createCell(headerRow2, 10, "单号: " + header.getOrderNo(),false);

                    // Create table headers
                    CellStyle margeCellStyle = workbook.createCellStyle();
                    margeCellStyle.setBorderBottom(BorderStyle.THIN);
                    margeCellStyle.setBorderTop(BorderStyle.THIN);
                    margeCellStyle.setBorderLeft(BorderStyle.THIN);
                    margeCellStyle.setBorderRight(BorderStyle.THIN);
                    margeCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
                    margeCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
                    margeCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
                    margeCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
                    CellUtil.mergingCells(sheet, 3, 4, 0, 0, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 1, 1, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 2, 2, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 3, 3, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 4, 4, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 5, 5, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 6, 6, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 7, 7, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 8, 8, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 9, 9, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 11, 11, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 12, 12, margeCellStyle);
                    Row tableHeaderRow = sheet.createRow(3);
                    Row tableHeaderRow2 = sheet.createRow(4);
                    createCell(tableHeaderRow, 0, "序号",true);
                    createCell(tableHeaderRow, 1, "品名",true);
                    createCell(tableHeaderRow, 2, "规格",true);
                    createCell(tableHeaderRow, 3, "数量",true);
                    createCell(tableHeaderRow, 4, "单价",true);
                    createCell(tableHeaderRow, 5, "总价",true);
                    createCell(tableHeaderRow, 6, "生产企业/委托生产企业",true);
                    createCell(tableHeaderRow, 7, "委托厂家",true);
                    createCell(tableHeaderRow, 8, "生产企业许可证",true);
                    createCell(tableHeaderRow, 9, "批号(序列号)",true);
                    createCell(tableHeaderRow, 10, "生产日期",true);
                    createCell(tableHeaderRow, 11, "注册证号/备案凭证编号",true);
                    createCell(tableHeaderRow, 12, "储运条件",true);
                    createCell(tableHeaderRow2, 0, null,true);
                    createCell(tableHeaderRow2, 1, null,true);
                    createCell(tableHeaderRow2, 2, null,true);
                    createCell(tableHeaderRow2, 3, null,true);
                    createCell(tableHeaderRow2, 4, null,true);
                    createCell(tableHeaderRow2, 5, null,true);
                    createCell(tableHeaderRow2, 6, null,true);
                    createCell(tableHeaderRow2, 7, null,true);
                    createCell(tableHeaderRow2, 8, null,true);
                    createCell(tableHeaderRow2, 9, null,true);
                    createCell(tableHeaderRow2, 10, "有效期",true);
                    createCell(tableHeaderRow2, 11, null,true);
                    createCell(tableHeaderRow2, 12, null,true);
                    currentRowIndex = dataRowIndex; // 重置当前行索引
                    // Add QR Code
                    ByteArrayOutputStream os = generateQRCode(header.getOrderNo());
                    byte[] qrCodeBytes = os.toByteArray();
                    InputStream is = new ByteArrayInputStream(qrCodeBytes);
                    Drawing<?> drawing = sheet.createDrawingPatriarch();
                    ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 11, 0, 13, 3);
                    Picture pict = drawing.createPicture(anchor, workbook.addPicture(IOUtils.toByteArray(is), Workbook.PICTURE_TYPE_PNG));
                    // Add End of Page
                    Row row = sheet.createRow(5 + (2*pageSize));
                    CellUtil.mergingCells(sheet,5+(2*pageSize),5+(2*pageSize),0,1,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize),5+(2*pageSize),2,8,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize),5+(2*pageSize),9,10,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize),5+(2*pageSize),11,12,margeCellStyle);
                    createCell(row,0,"本页金额（大写）：",true);
                    createCell(row,2,header.getPageCoinNum(),true);
                    createCell(row,9,"本页小计：",true);
                    createCell(row,11, header.getPagecountS(),true);
                    Row row2 = sheet.createRow(5 + (2*pageSize) + 1);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+1,5+(2*pageSize)+1,0,1,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+1,5+(2*pageSize)+1,2,8,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+1,5+(2*pageSize)+1,9,10,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+1,5+(2*pageSize)+1,11,12,margeCellStyle);
                    createCell(row2,0,"整页合计（大写）：",true);
                    createCell(row2,2,header.getTotalCount(),true);
                    createCell(row2,9,"合计（小写）：",true);
                    createCell(row2,11, header.getAllTotalCount(),true);
                    Row row3 = sheet.createRow(5 + (2*pageSize) + 2);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+2,5+(2*pageSize)+2,0,1,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+2,5+(2*pageSize)+2,2,8,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+2,5+(2*pageSize)+2,9,10,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+2,5+(2*pageSize)+2,11,12,margeCellStyle);
                    createCell(row3,0,"整单数量：",true);
                    createCell(row3,2,header.getTotalNum(),true);
                    createCell(row3,9,"本页数量：",true);
                    createCell(row3,11, header.getPageCountNum(),true);
                } else if (i == 0) {
                    sheet = workbook.createSheet("Invoice");
                    sheet.setColumnWidth(0,15*256);
                    sheet.setColumnWidth(5,30*256);
                    sheet.setColumnWidth(6,30*256);
                    sheet.setColumnWidth(7,30*256);
                    sheet.setColumnWidth(8,15*256);
                    sheet.setColumnWidth(9,15*256);
                    sheet.setColumnWidth(10,15*256);
                    Row row0 = sheet.createRow(0);
                    row0.setHeight((short) (30 * 20));
                    CellUtil.mergingCells(sheet, 0, 0, 0, 12, null);
                    Cell cell = row0.createCell(0);
                    CellStyle cellStyle = workbook.createCellStyle();
                    cellStyle.setAlignment(HorizontalAlignment.CENTER);
                    Font font = workbook.createFont();
                    font.setBold(true);
                    font.setFontHeightInPoints((short) 22);
                    cellStyle.setFont(font);
                    cell.setCellValue("供应链管理有限公司销售出库单");
                    cell.setCellStyle(cellStyle);
                    // Write header information
                    Row headerRow = sheet.createRow(1);
                    headerRow.setHeight((short) (25 * 20));
                    CellUtil.mergingCells(sheet, 1, 1, 1, 5, null);
                    createCell(headerRow, 0, "开票日期: ",false);
                    createCell(headerRow, 1, header.getInvoiceDate(),false);
                    createCell(headerRow, 10, "第 1/"+pageNum + " 页",false);
                    Row headerRow2 = sheet.createRow(2);
                    headerRow2.setHeight((short) (25 * 20));
                    CellUtil.mergingCells(sheet, 2, 2, 1, 5, null);
                    createCell(headerRow2, 0, "单位名称: ",false);
                    createCell(headerRow2, 1,  header.getRecipient(),false);
                    createCell(headerRow2, 7, "收货地址: " + header.getAddress(),false);
                    createCell(headerRow2, 10, "单号: " + header.getOrderNo(),false);

                    // Create table headers
                    CellStyle margeCellStyle = workbook.createCellStyle();
                    margeCellStyle.setBorderBottom(BorderStyle.THIN);
                    margeCellStyle.setBorderTop(BorderStyle.THIN);
                    margeCellStyle.setBorderLeft(BorderStyle.THIN);
                    margeCellStyle.setBorderRight(BorderStyle.THIN);
                    margeCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
                    margeCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
                    margeCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
                    margeCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
                    CellUtil.mergingCells(sheet, 3, 4, 0, 0, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 1, 1, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 2, 2, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 3, 3, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 4, 4, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 5, 5, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 6, 6, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 7, 7, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 8, 8, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 9, 9, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 11, 11, margeCellStyle);
                    CellUtil.mergingCells(sheet, 3, 4, 12, 12, margeCellStyle);
                    Row tableHeaderRow = sheet.createRow(3);
                    Row tableHeaderRow2 = sheet.createRow(4);
                    createCell(tableHeaderRow, 0, "序号",true);
                    createCell(tableHeaderRow, 1, "品名",true);
                    createCell(tableHeaderRow, 2, "规格",true);
                    createCell(tableHeaderRow, 3, "数量",true);
                    createCell(tableHeaderRow, 4, "单价",true);
                    createCell(tableHeaderRow, 5, "总价",true);
                    createCell(tableHeaderRow, 6, "生产企业/委托生产企业",true);
                    createCell(tableHeaderRow, 7, "委托厂家",true);
                    createCell(tableHeaderRow, 8, "生产企业许可证",true);
                    createCell(tableHeaderRow, 9, "批号(序列号)",true);
                    createCell(tableHeaderRow, 10, "生产日期",true);
                    createCell(tableHeaderRow, 11, "注册证号/备案凭证编号",true);
                    createCell(tableHeaderRow, 12, "储运条件",true);
                    createCell(tableHeaderRow2, 0, null,true);
                    createCell(tableHeaderRow2, 1, null,true);
                    createCell(tableHeaderRow2, 2, null,true);
                    createCell(tableHeaderRow2, 3, null,true);
                    createCell(tableHeaderRow2, 4, null,true);
                    createCell(tableHeaderRow2, 5, null,true);
                    createCell(tableHeaderRow2, 6, null,true);
                    createCell(tableHeaderRow2, 7, null,true);
                    createCell(tableHeaderRow2, 8, null,true);
                    createCell(tableHeaderRow2, 9, null,true);
                    createCell(tableHeaderRow2, 10, "有效期",true);
                    createCell(tableHeaderRow2, 11, null,true);
                    createCell(tableHeaderRow2, 12, null,true);
                    currentRowIndex = dataRowIndex; // 重置当前行索引
                    // Add QR Code
                    ByteArrayOutputStream os = generateQRCode(header.getOrderNo());
                    byte[] qrCodeBytes = os.toByteArray();
                    InputStream is = new ByteArrayInputStream(qrCodeBytes);
                    Drawing<?> drawing = sheet.createDrawingPatriarch();
                    ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 11, 0, 13, 3);
                    Picture pict = drawing.createPicture(anchor, workbook.addPicture(IOUtils.toByteArray(is), Workbook.PICTURE_TYPE_PNG));
                    // Add End of Page
                    Row row = sheet.createRow(5 + (2*pageSize));
                    CellUtil.mergingCells(sheet,5+(2*pageSize),5+(2*pageSize),0,1,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize),5+(2*pageSize),2,8,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize),5+(2*pageSize),9,10,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize),5+(2*pageSize),11,12,margeCellStyle);
                    createCell(row,0,"本页金额（大写）：",true);
                    createCell(row,2,header.getPageCoinNum(),true);
                    createCell(row,9,"本页小计：",true);
                    createCell(row,11, header.getPagecountS(),true);
                    Row row2 = sheet.createRow(5 + (2*pageSize) + 1);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+1,5+(2*pageSize)+1,0,1,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+1,5+(2*pageSize)+1,2,8,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+1,5+(2*pageSize)+1,9,10,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+1,5+(2*pageSize)+1,11,12,margeCellStyle);
                    createCell(row2,0,"整页合计（大写）：",true);
                    createCell(row2,2,header.getTotalCount(),true);
                    createCell(row2,9,"合计（小写）：",true);
                    createCell(row2,11, header.getAllTotalCount(),true);
                    Row row3 = sheet.createRow(5 + (2*pageSize) + 2);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+2,5+(2*pageSize)+2,0,1,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+2,5+(2*pageSize)+2,2,8,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+2,5+(2*pageSize)+2,9,10,margeCellStyle);
                    CellUtil.mergingCells(sheet,5+(2*pageSize)+2,5+(2*pageSize)+2,11,12,margeCellStyle);
                    createCell(row3,0,"整单数量：",true);
                    createCell(row3,2,header.getTotalNum(),true);
                    createCell(row3,9,"本页数量：",true);
                    createCell(row3,11, header.getPageCountNum(),true);
                }

                // Start a new page

                Row dateRow = sheet.createRow(currentRowIndex);
                Row dateRow2 = sheet.createRow(currentRowIndex + 1);
                CellStyle margeCellStyle = workbook.createCellStyle();
                margeCellStyle.setBorderBottom(BorderStyle.THIN);
                margeCellStyle.setBorderTop(BorderStyle.THIN);
                margeCellStyle.setBorderLeft(BorderStyle.THIN);
                margeCellStyle.setBorderRight(BorderStyle.THIN);
                margeCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
                margeCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
                margeCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
                margeCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
                CellUtil.mergingCells(sheet, currentRowIndex, currentRowIndex + 1, 0, 0, margeCellStyle);
                CellUtil.mergingCells(sheet, currentRowIndex, currentRowIndex + 1, 1, 1, margeCellStyle);
                CellUtil.mergingCells(sheet, currentRowIndex, currentRowIndex + 1, 2, 2, margeCellStyle);
                CellUtil.mergingCells(sheet, currentRowIndex, currentRowIndex + 1, 3, 3, margeCellStyle);
                CellUtil.mergingCells(sheet, currentRowIndex, currentRowIndex + 1, 4, 4, margeCellStyle);
                CellUtil.mergingCells(sheet, currentRowIndex, currentRowIndex + 1, 5, 5, margeCellStyle);
                CellUtil.mergingCells(sheet, currentRowIndex, currentRowIndex + 1, 6, 6, margeCellStyle);
                CellUtil.mergingCells(sheet, currentRowIndex, currentRowIndex + 1, 7, 7, margeCellStyle);
                CellUtil.mergingCells(sheet, currentRowIndex, currentRowIndex + 1, 8, 8, margeCellStyle);
                CellUtil.mergingCells(sheet, currentRowIndex, currentRowIndex + 1, 9, 9, margeCellStyle);
                CellUtil.mergingCells(sheet, currentRowIndex, currentRowIndex + 1, 11, 11, margeCellStyle);
                CellUtil.mergingCells(sheet, currentRowIndex, currentRowIndex + 1, 12, 12, margeCellStyle);
                createCell(dateRow, 0, items.get(i).getId(),true);
                createCell(dateRow, 1, items.get(i).getProductName(),true);
                createCell(dateRow, 2, items.get(i).getSpecification(),true);
                createCell(dateRow, 3, items.get(i).getQuantity(),true);
                createCell(dateRow, 4, items.get(i).getUnitPrice(),true);
                createCell(dateRow, 5, items.get(i).getTotal(),true);
                createCell(dateRow, 6, items.get(i).getEntrustCompany(),true);
                createCell(dateRow, 7, items.get(i).getProductCompany(),true);
                createCell(dateRow, 8, items.get(i).getProductLicence(),true);
                createCell(dateRow, 9, items.get(i).getSerialNumber(),true);
                createCell(dateRow, 10, items.get(i).getManufactureDate(),true);
                createCell(dateRow, 11, items.get(i).getConsignee(),true);
                createCell(dateRow, 12, items.get(i).getConditions(),true);
                createCell(dateRow2, 0,null,true);
                createCell(dateRow2, 1, null,true);
                createCell(dateRow2, 2, null,true);
                createCell(dateRow2, 3, null,true);
                createCell(dateRow2, 4, null,true);
                createCell(dateRow2, 5, null,true);
                createCell(dateRow2, 6, null,true);
                createCell(dateRow2, 7, null,true);
                createCell(dateRow2, 8, null,true);
                createCell(dateRow2, 9, null,true);
                createCell(dateRow2, 10, items.get(i).getExpirationDate(),true);
                createCell(dateRow2, 11, null,true);
                createCell(dateRow2, 12, null,true);
                currentRowIndex += 2;


            }
            // Save the Excel file
            try (FileOutputStream fileOut = new FileOutputStream("D:\\JAVA\\poi\\测试模板导出.xlsx")) {
                workbook.write(fileOut);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void exportToPdf(InvoiceHeader header, List<InvoiceItem> items, int pageSize) throws IOException {
        PdfWriter writer = new PdfWriter("D:\\JAVA\\poi\\测试模板导出.pdf");
        PdfDocument pdfDoc = new PdfDocument(writer);
        Document document = new Document(pdfDoc);

        // Write header information
        document.add(new Paragraph("开票日期: " + header.getInvoiceDate()));
        document.add(new Paragraph("收货人: " + header.getRecipient()));
        document.add(new Paragraph("单号: " + header.getOrderNo()));

        // Create table headers
        Table table = new Table(new float[]{1, 1, 1, 1, 1});
        table.addCell("品名");
        table.addCell("规格");
        table.addCell("数量");
        table.addCell("单价");
        table.addCell("总价");

        // Write data rows
        for (int i = 0; i < items.size(); i++) {
            if (i > 0 && i % pageSize == 0) {
                // Start a new page
                document.flush();
                document.add(new Paragraph("开票日期: " + header.getInvoiceDate()));
                document.add(new Paragraph("收货人: " + header.getRecipient()));
                document.add(new Paragraph("单号: " + header.getOrderNo()));
                table = new Table(new float[]{1, 1, 1, 1, 1});
                table.addCell("品名");
                table.addCell("规格");
                table.addCell("数量");
                table.addCell("单价");
                table.addCell("总价");
            }
            InvoiceItem item = items.get(i);
            table.addCell(item.getProductName());
            table.addCell(item.getSpecification());
            table.addCell(String.valueOf(item.getQuantity()));
            table.addCell(String.valueOf(item.getUnitPrice()));
            table.addCell(String.valueOf(item.getTotal()));
        }
        document.add(table);

        // Add QR Code
        ByteArrayOutputStream os = null;
        try {
            os = generateQRCode(header.getOrderNo());
        } catch (WriterException e) {
            e.printStackTrace();
        }
        ImageData imageData = ImageDataFactory.create(os.toByteArray());
        Image img = new Image(imageData);
        document.add(img);

        document.close();
    }

    private org.apache.poi.ss.usermodel.Cell createCell(Row row, int column, String value,boolean haveBorder) {
        CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
        org.apache.poi.ss.usermodel.Cell cell = row.createCell(column);
        cell.setCellValue(value);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        if (haveBorder){
            // 设置边框样式
            cellStyle.setBorderTop(BorderStyle.THIN); // 上边框
            cellStyle.setBorderBottom(BorderStyle.THIN); // 下边框
            cellStyle.setBorderLeft(BorderStyle.THIN); // 左边框
            cellStyle.setBorderRight(BorderStyle.THIN); // 右边框
            // 设置边框颜色为黑色
            cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
            cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
            cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
            cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
        }
        cell.setCellStyle(cellStyle);
        return cell;
    }

    private ByteArrayOutputStream generateQRCode(String orderNo) throws WriterException, IOException {
        String qrContent = "http://www.baiud.con?orderNo=" + orderNo;
        BitMatrix matrix = new QRCodeWriter().encode(qrContent, BarcodeFormat.QR_CODE, 200, 200);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(matrix, "png", os);
        return os;
    }
}